package com.cgsrp.cgsrp_baby.service.impl;

import com.cgsrp.cgsrp_baby.dao.BabyMapper;
import com.cgsrp.cgsrp_baby.dao.ChildParentMapper;
import com.cgsrp.cgsrp_baby.dao.MessageMapper;
import com.cgsrp.cgsrp_baby.domain.Baby;
import com.cgsrp.cgsrp_baby.domain.ChildParent;
import com.cgsrp.cgsrp_baby.domain.Message;
import com.cgsrp.cgsrp_baby.service.BabyService;
import com.cgsrp.cgsrp_user_province.domain.User;
import com.cgsrp.cgsrp_user_province.service.UserService;
import com.cgsrp.utils.Result;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.cgsrp.cgsrp_baby.dao.ChildParentRelationMapper;
import com.cgsrp.cgsrp_baby.domain.ChildParentRelation;
import com.cgsrp.cgsrp_baby.service.ChildParentRelationService;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
public class ChildParentRelationServiceImpl implements ChildParentRelationService{

    @Resource
    private ChildParentRelationMapper childParentRelationMapper;

    @Resource
    private UserService userService;

    @Resource
    private BabyMapper babyMapper;

    @Resource
    private MessageMapper messageMapper;

    @Resource
    private ChildParentMapper childParentMapper;

    @Resource
    private RedisTemplate redisTemplate;


    @Resource
    private RestHighLevelClient restHighLevelClient;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return childParentRelationMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(ChildParentRelation record) {
        return childParentRelationMapper.insert(record);
    }

    @Override
    public int insertSelective(ChildParentRelation record) {
        return childParentRelationMapper.insertSelective(record);
    }

    @Override
    public ChildParentRelation selectByPrimaryKey(Integer id) {
        return childParentRelationMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(ChildParentRelation record) {
        return childParentRelationMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(ChildParentRelation record) {
        return childParentRelationMapper.updateByPrimaryKey(record);
    }

    @Override
    public boolean add(int babyId, String phone, String relationship, String name) {
        User user = userService.findByPhone(phone);
        Baby baby = babyMapper.selectByPrimaryKey(babyId);
        ChildParentRelation childParentRelation = new ChildParentRelation();
        childParentRelation.setBabyId(babyId);
        childParentRelation.setType(1);
        childParentRelation.setRelationship(relationship);
        childParentRelation.setUsername(user.getUsername());
        childParentRelation.setUserId(user.getId());
        childParentRelation.setMessage(name+"邀请您成为"+baby.getName()+"("+baby.getPetName()+")的"+relationship);
        childParentRelationMapper.insert(childParentRelation);
        Message message = new Message();
        message.setMessage(name+"邀请您成为"+baby.getName()+"("+baby.getPetName()+")的"+relationship);
        message.setType(0);
        message.setState(0);
        message.setCreatby(name);
        message.setCreattime(new Date());
        message.setUserId(user.getId());
        message.setRelationId(childParentRelation.getId());
        return messageMapper.insert(message)>0;
    }

    @Override
    public boolean del(int childId, int parentId,String name) {
        User user = userService.selectByPrimaryKey(parentId);
        Baby baby = babyMapper.selectByPrimaryKey(childId);
        ChildParentRelation childParentRelation = new ChildParentRelation();
        childParentRelation.setBabyId(childId);
        childParentRelation.setType(2);
        childParentRelation.setRelationship("解除关系");
        childParentRelation.setUsername(user.getUsername());
        childParentRelation.setUserId(user.getId());
        childParentRelation.setMessage(name+"请求您解除与"+baby.getName()+"("+baby.getPetName()+")的关系");
        childParentRelationMapper.insert(childParentRelation);
        Message message = new Message();
        message.setMessage(name+"请求您解除与"+baby.getName()+"("+baby.getPetName()+")的关系");
        message.setType(0);
        message.setState(0);
        message.setCreatby(name);
        message.setCreattime(new Date());
        message.setUserId(user.getId());
        message.setRelationId(childParentRelation.getId());
        return messageMapper.insert(message)>0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result accept(Message message) {
        Integer relationId = message.getRelationId();
        ChildParentRelation childParentRelation = childParentRelationMapper.selectByPrimaryKey(relationId);
        if (childParentRelation.getType()==1){
            ChildParent childParent = new ChildParent();
            childParent.setParentId(childParentRelation.getUserId());
            childParent.setChildId(childParentRelation.getBabyId());
            childParent.setRelationship(childParentRelation.getRelationship());
            childParentMapper.insertSelective(childParent);
        }else {
            childParentMapper.deleteByPrimaryKey(childParentRelation.getBabyId(),childParentRelation.getUserId());
        }
        DeleteIndexRequest request = new DeleteIndexRequest("es"+childParentRelation.getUserId());
        try {
            restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        redisTemplate.delete("REDIS"+childParentRelation.getUserId());
        return messageMapper.updateState(message.getId(),1)>0?Result.success("已接受"):Result.failure("系统故障");
    }

}
